/**
 * 
 */
package com.hwadee.JPKC.controller;


import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hwadee.JPKC.entity.comment;
import com.hwadee.JPKC.entity.courseInfo;
import com.hwadee.JPKC.entity.courseView;
import com.hwadee.JPKC.service.CommentService;
//import com.hwadee.JPKC.entity.courseview;
import com.hwadee.JPKC.service.CourseInfoService;
import com.hwadee.JPKC.service.courseViewService;

/**

* @ClassName: CourseInfoController

* @Description: TODO(课程信息控制器)

* @author wuhaizhen

* @date 2022-01-19


*/
@Controller
@RequestMapping("/Info")
public class CourseInfoController {
	
	@Autowired
	private CourseInfoService courseInfoService;
	@Autowired
	private courseViewService courseviewservice;
	@Autowired
	private CommentService conmmentservice;
	

	
	/**
	 * 方法：xiugaiInfo 修改课程状态
	 * @param courseInfo
	 * @return String
	 * @date 2020-03-07
	 */
	@RequestMapping("/xiugaiInfo")
	public String xiugaiInfo(courseInfo courseInfo) {
		courseInfo.setAcademicExchange("已上线");
		courseInfoService.updateByPrimaryKeySelective(courseInfo);
		return "redirect:/admin/courseInfo.ht";
	}
	/*
	 *  显示对应课程类型1和类型2的 课程介绍页
	 */
	@RequestMapping("/courseDesciption")
	public String courseDesciption(Integer courseId,Model model) {
		courseInfo ci = courseInfoService.selectByPrimaryKey(courseId);
		if(ci.getAcademicExchange().equals("已上线")) {
//			courseView cv = courseviewservice.FindByID(vId);
			List<courseView> cv  = courseviewservice.FindAll(courseId);
			System.err.println(ci.toString());
			//模糊查询
			List<courseInfo> cis = courseInfoService.selectcourseInfosMohu(ci.getCourseDescription());
			System.err.println(cis.toString());
			model.addAttribute("cis", cis);
			model.addAttribute("ci", ci);
			model.addAttribute("cv", cv);
			return "courseDesciption";
		}else{
			model.addAttribute("msg", "没上线!!");
			return "redirect:/index/";
		}
		

	}
	
	/*
	 * 课程详情页
	 */
	@RequestMapping("/courseInfos")
	public String courseInfo(@RequestParam(name = "p", required = true, defaultValue = "1") Integer p,courseInfo courseInfo,Integer courseId,Model model) {
		
		
		courseInfo ci = courseInfoService.selectByPrimaryKey(courseId);
		System.out.println(ci.toString());
		if(ci.getCourseNum()>=0) {
			int num = ci.getCourseNum()+1;
			ci.setCourseNum(num);
			System.err.println(num);
			courseInfoService.updateByPrimaryKeySelective(ci);
		}
		
		Page<courseView> courseViews = PageHelper.startPage(p, 5).doSelectPage(()->courseviewservice.FindAll(courseId));

		//单门课程的所有评论
		List<comment> cm  = conmmentservice.selectCourseId(courseId);

		model.addAttribute("ci", ci);
		model.addAttribute("courseViews", courseViews);
		model.addAttribute("cm", cm);
		return "courseInfo";
	}
	
	@RequestMapping("/shipin")
	public @ResponseBody courseView shipin(int vId,Model model) {
		courseView courseViews =courseviewservice.FindByID(vId);
		return courseViews;
	}

}
